package com.cipe.cmrs.util;

import java.security.MessageDigest;



/**
 * TODO: Consider using this class ONLY inside the centralized-manager {@link UserService} class;  
 * 			all other uses of this class should go through the UserService class methods.
 */

public class PasswordEncryptionUtil {

	public static String SINGLE_QUOTE_ENCRYPTION = "265FDA17A34611B1533D8A281FF680DC5791B0CE0A11C25B35E11C8E75685509";
	public static String getEncryptedPassword(String plainTextPassword) {
//	 check for null or zero length password
   if (Util.isNullOrEmpty(plainTextPassword)) {
         return plainTextPassword;
   }
   StringBuilder sb = new StringBuilder();
   try {
	    MessageDigest md = MessageDigest.getInstance("SHA-256");
	    md.update(plainTextPassword.getBytes("UTF-8"));
	    byte[] encryped = md.digest();
	    String hex = null;
	    
	    // let's convert the bytes to hex code...
	    for (int i = 0; i < encryped.length; i++) {
	    	hex = Integer.toHexString(0xFF & encryped[i]);
				if (hex.length() < 2) {
				    sb.append("0");
				}
			  sb.append(hex.toUpperCase());
	    } // for (int i = 0; i < encryped.length; i++)
	  }
	  catch (Exception e) {
				e.printStackTrace();		// NOT swallowing exception!
		}
   return sb.toString();
	}

}
